package com.wanjia.bpsapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanjia.bpsapi.annotation.LoginToken;
import com.wanjia.bpsapi.exception.DataExistException;
import com.wanjia.bpsapi.exception.DeleteException;
import com.wanjia.bpsapi.exception.UpdateException;
import com.wanjia.bpsapi.pojo.dto.DefaultDataDto;
import com.wanjia.bpsapi.pojo.dto.VegeParamTable;
import com.wanjia.bpsapi.pojo.entity.DefaultData;
import com.wanjia.bpsapi.pojo.model.JsonResult;
import com.wanjia.bpsapi.service.IDefaultDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//默认数据表接口组
@RestController
@Tag(name = "默认数据接口控制器")
public class DefaultController extends BaseController{

    @Autowired
    IDefaultDataService defaultDataService;

    //获取参数项数据接口
    @GetMapping("/default/getlist")
    @Operation(summary = "获取参数项数据接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<Object> getDefaultData(@Parameter(name = "uid") Integer uid , Integer pageNo, Integer pageSize) {
        Page<DefaultDataDto> page = new Page<>(pageNo, pageSize);
        List<DefaultDataDto> data = defaultDataService.getData(uid, pageNo, pageSize);
        int size = data.size();
        page.setTotal(size);
        long pages = size / pageSize;
        if (size - pages * pageSize > 0) {
            pages++;
        }
        page.setRecords(data);
        page.setPages(pages);
        return new JsonResult<>(OK,page);
    }


    @PostMapping("/default")
    @Operation(summary = "新增参数项数据接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult addDefaultData(@RequestBody DefaultData defaultData){
        Long uid = defaultData.getUid();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("uid",uid);
        DefaultData byId = defaultDataService.getOne(wrapper);
        if (byId != null){
            throw new DataExistException("此ID数据已存在，不能新增，可修改数据");
        }
        defaultDataService.save(defaultData);
        return new JsonResult(OK);
    }

    @PutMapping("/default")
    @Operation(summary = "修改参数项数据接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult updateDefaultData(@RequestBody DefaultData defaultData){
        int i = defaultDataService.updateData(defaultData);
        if (i!=1){
            throw new UpdateException("更新数据发生错误");
        }
        return new JsonResult(OK);
    }


    @DeleteMapping("/default")
    @Operation(summary = "删除参数项数据接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult deleteDefaultData(Integer id){
        int i = defaultDataService.deleteData(id);
        if (i!=1){
            throw new DeleteException("删除数据发生错误");
        }
        return new JsonResult(OK);
    }
}
